package com.nowcoder.community.controller;

import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.MyPage;
import com.nowcoder.community.entity.PageEntity;
import com.nowcoder.community.service.impl.ElasticSearchServiceImpl;
import com.nowcoder.community.service.impl.LikeServiceImpl;
import com.nowcoder.community.service.impl.UserServiceImpl;
import com.nowcoder.community.util.CommunityConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cjh
 * @date 2023/2/1 - 15:04
 */
@Controller
public class SearchController implements CommunityConstant {
    @Autowired
    private ElasticSearchServiceImpl elasticSearchService;

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private LikeServiceImpl likeService;


    //    search?keyword=***   两种实现方式  因为是get 问号 或者路径传参
    @RequestMapping(path = "/search", method = RequestMethod.GET)
    public String search(String keyword, PageEntity pageEntity, Model model) {
//        搜索帖子

//        注意 因为 elastic的分页起止 是从0开始的 所以-1
        Page<DiscussPost> searchResult = elasticSearchService.searchDiscussPost
                (keyword, (int) pageEntity.getCurrent()-1, (int) pageEntity.getSize());
//        聚合数据
        List<Map<String, Object>> discussPosts = new ArrayList<>();
        if (searchResult != null) {
            for (DiscussPost post : searchResult) {
                HashMap<String, Object> map = new HashMap<>();
//              帖子
                map.put("post", post);

                System.out.println(post);

//                作者
                map.put("user", userService.getById(post.getUserId()));
//                点赞数量
                map.put("likeCount", likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId()));

                discussPosts.add(map);
            }
        }
        model.addAttribute("discussPosts", discussPosts);
        model.addAttribute("keyword", keyword);

//        分页信息
        MyPage page = new MyPage();
        page.setCurrent((int) pageEntity.getCurrent());
        page.setSize((int) pageEntity.getSize());
        page.setTotal(searchResult == null ? 0 : (int) searchResult.getTotalElements());
        page.setPages(searchResult == null ? 0 : searchResult.getTotalPages());
        //        需要封装的内容
        model.addAttribute("page", page);
        long from = page.getCurrent() - 2;
        from = from < 1 ? 1 : from;
        long to = page.getCurrent() + 2;
        to = Math.min(to, page.getPages());
        model.addAttribute("from", from);
        model.addAttribute("to", to);
        model.addAttribute("path", "/search?keyword=" + keyword);

        return "/site/search";
    }

}
